﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace FeedAnimal
{
    public partial class Form1 : Form
    {
        private readonly DbFeedAnimalDataContext _dbContext = new DbFeedAnimalDataContext();

        public Form1()
        {
            InitializeComponent();
            LoadAnimalDropDownList();
        }

        #region NguyenLieu

        public void LoadAnimalDropDownList()
        {
            IQueryable<TbAnimal> animalName = from animal in _dbContext.TbAnimals select animal;
            foreach (TbAnimal animal in animalName)
            {
                cbTenVatNuoiNguyenLieu.Items.Add(animal.Name);
                cbTenVatNuoiPhoiTron.Items.Add(animal.Name);
                cbTenVatNuoiCongThuc.Items.Add(animal.Name);
            }
        }

        private void cbTenVatNuoiNguyenLieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvNguyenLieu.ClearSelection();
            string animalName = cbTenVatNuoiNguyenLieu.SelectedItem.ToString();
            int animalId =
                (from animal in _dbContext.TbAnimals where animal.Name == animalName select animal.Id).First();
            var ingredientThanhPhan = from a in _dbContext.TbAnimals
                                      from i in _dbContext.TbIngredients
                                      from ic in _dbContext.TbIngredientCategories
                                      where a.Id == animalId &&
                                            i.AnimalId == a.Id &&
                                            i.CategoryId == ic.Id
                                      select new
                                      {
                                          TenNguyenLieu = i.Name,
                                          MaSo = i.Id,
                                          Gia = i.Price,
                                          DoKho = i.Dry,
                                          PhanLoai = ic.Name,
                                          TrongKho = i.InStock
                                      };


            gvNguyenLieu.DataSource = ingredientThanhPhan;
        }

        #endregion

        #region PhoiTron

        private List<int> formulaManagerId;

        private void cbTenVatNuoiPhoiTron_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvPhoiTron.ClearSelection();
            string animalName = cbTenVatNuoiPhoiTron.SelectedItem.ToString();
            int animalId =
                (from animal in _dbContext.TbAnimals where animal.Name == animalName select animal.Id).First();
            IQueryable<string> animalFormula =
                from a in _dbContext.TbAnimals
                from f in _dbContext.TbFormulaManagers
                where a.Id == f.AnimalId &&
                      a.Id == animalId
                select f.Name;
            cbTenCongThucPhoiTron.DataSource = animalFormula;
            dataGridView2.DataSource = null;
        }

        private void cbTenCongThucPhoiTron_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvPhoiTron.ClearSelection();
            string congThuc = cbTenCongThucPhoiTron.SelectedItem.ToString();
            int formulaId = (from formula in _dbContext.TbFormulaManagers
                             where formula.Name == congThuc
                             select formula.Id).First();
            IQueryable<string> nguyenLieu =
                from i in _dbContext.TbIngredientTables
                from m in _dbContext.TbFormulaManagers
                where m.Id == i.FormulaId &&
                      m.Id == formulaId
                select i.IngredientTableName;
            cbBangNguyenLieuPhoiTron.DataSource = nguyenLieu;
        }

        private void cbBangNguyenLieuPhoiTron_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView2.DataSource = null;
            string bangNguyenLieu = cbBangNguyenLieuPhoiTron.SelectedItem.ToString();

            string congThuc = cbTenCongThucPhoiTron.SelectedItem.ToString();
            int formulaId = (from formula in _dbContext.TbFormulaManagers
                             where formula.Name == congThuc
                             select formula.Id).First();

            var unknown = from m in _dbContext.TbFormulaManagers
                          from it in _dbContext.TbIngredientTables
                          from f in _dbContext.TbFormulaIngredientItems
                          from i in _dbContext.TbIngredients
                          where m.Id == it.FormulaId &&
                                m.Id == formulaId &&
                                it.IngredientTableName == bangNguyenLieu &&
                                it.Id == f.IngredientTableId &&
                                i.Id == f.IngredientId
                          select new
                          {
                              FormulaId = m.Id,
                              TenNguyenLieu = i.Name,
                              MaSo = i.Code,
                              Gia = i.Price,
                              DoKho = i.Dry,
                              ThapNhat = f.MinValue,
                              CaoNhat = f.MaxValue
                          };

            gvPhoiTron.DataSource = unknown;

            formulaManagerId = new List<int>();
            foreach (var u in unknown)
            {
                formulaManagerId.Add(u.FormulaId);
            }
            Dummy();
        }

        private void Dummy()
        {
            foreach (int i in formulaManagerId)
            {
                int i1 = i;
                var d =
                    from fn in _dbContext.TbFormulaNutrientItems
                    from ni in _dbContext.TbNutrientIndexes
                    from nt in _dbContext.TbNutrientTypes
                    from nu in _dbContext.TbNutrientUnits
                    where fn.FormulaId == i1 &&
                          fn.NutrientId == ni.Id &&
                          nu.Id == ni.UnitId &&
                          nt.Id == ni.TypeId
                    select new
                    {
                        Id = fn.NutrientId,
                        NutrientName = ni.Name,
                        NutrientCode = ni.Code,
                        NutrientType = nt.Name,
                        NutrientUnit = nu.Name,
                        Min = fn.MinValue,
                        Max = fn.MaxValue
                    };
                dataGridView2.DataSource = d;
            }
        }

        #endregion
        #region CongThuc

        private void cbTenVatNuoiCongThuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCongThuc();
        }
        public void LoadCongThuc()
        {
            gvCongThuc.ClearSelection();
            string animalName = cbTenVatNuoiCongThuc.SelectedItem.ToString();
            int animalId =
                (from animal in _dbContext.TbAnimals where animal.Name == animalName select animal.Id).First();
            var formulaManage = from a in _dbContext.TbAnimals
                                from f in _dbContext.TbFormulaManagers
                                where a.Id == animalId &&
                                      f.AnimalId == a.Id
                                select new
                                {
                                    Stt = f.Id,
                                    TênCôngThức = f.Name,
                                    MãSố = f.Code,
                                    MôTả = f.Description,
                                    NgàyTạo = f.CreateDate,
                                    NgàyThayĐổi = f.ModifyDate
                                };
            gvCongThuc.DataSource = formulaManage;
        }

        private void btnThemMoiCongThuc_Click(object sender, EventArgs e)
        {
            //this.Enabled = false;
            frmThemMoiCT frmThemMoi = new frmThemMoiCT();
            frmThemMoi.Show();
        }

        private void btnThayDoiCongThuc_Click(object sender, EventArgs e)
        {

        }

        private void btnXoaCongThuc_Click(object sender, EventArgs e)
        {
            if (gvCongThuc.SelectedRows.Count > 0)
            {
                var deleteFormula =
                    from formular in _dbContext.TbFormulaManagers
                    where formular.Id.Equals(gvCongThuc.CurrentRow.Cells[0].Value)
                    select formular;
                DialogResult dialogResult = MessageBox.Show("Xác nhận muốn xóa!", "Xóa công thức", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    foreach (var formular in deleteFormula)
                    {
                        _dbContext.TbFormulaManagers.DeleteOnSubmit(formular);
                    }
                    try
                    {
                        _dbContext.SubmitChanges();
                    }
                    catch (Exception ex)
                    {

                    }
                }
                else if(dialogResult== DialogResult.No)
                {

                }
            }
            else
            {
                MessageBox.Show("Chọn công thức để xóa!");
            }
            LoadCongThuc();
        }
        #endregion




        #region DuongChat

        #endregion
    }
}